package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.order.client.OrderInfoFeignClient;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author: 李旭
 * @create: 2021-09-14 14:33
 **/
@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private OrderInfoFeignClient orderInfoFeignClient;
    @Autowired
    private RabbitService rabbitService;

    //创建支付表    给财务进行使用   帐单计算  一张订单 （N个商品） 付钱一次
    @Override
    public PaymentInfo createPayment(Long orderId,PaymentType paymentType) {
        //幂等性问题
        //0:根据orderId 查询支付表信息是否已经存在了
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
                .eq("order_id", orderId));
        if (null == paymentInfo) {
            //1:不存在 根据订单ID查询订单信息
            OrderInfo orderInfo = orderInfoFeignClient.getOrderInfo(orderId);
            paymentInfo = new PaymentInfo();
            paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
            paymentInfo.setOrderId(orderInfo.getId());
            paymentInfo.setPaymentType(paymentType.name());
            paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
            paymentInfo.setSubject(orderInfo.getTradeBody());
            paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
            paymentInfo.setCreateTime(new Date());
            paymentInfoMapper.insert(paymentInfo);
        }
        //2:如果存在了 直接返回
        return paymentInfo;
    }


    //查询支付表
    @Override
    public PaymentInfo getPaymentInfo(String out_trade_no) {
        return paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
           .eq("out_trade_no",out_trade_no));
    }

    @Override
    public PaymentInfo getPaymentInfo(Long orderId) {
        return paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
                .eq("order_id",orderId));
    }

    //更新支付表
    @Override
    public void updatePaymentInfo(Map<String, String> paramsMap) {
        //1:查询 幂等性问题  本场景是好用的   异步通知 更新了   4m  10m 10m 2h 。。再次通知
        PaymentInfo paymentInfo = getPaymentInfo(paramsMap.get("out_trade_no"));
        if(null != paymentInfo && paymentInfo.getPaymentStatus().equals(PaymentStatus.UNPAID.name())){
            //状态
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            //流水
            paymentInfo.setTradeNo(paramsMap.get("trade_no"));
            //回调的时间  2021-09-15 10:15:44
            try {
                paymentInfo.setCallbackTime(new
                        SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(paramsMap.get("notify_time")));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //回调的内容
            paymentInfo.setCallbackContent(JSONObject.toJSONString(paramsMap));
            //更新支付表
            paymentInfoMapper.updateById(paymentInfo);
            //发消息
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,
                    MqConst.ROUTING_PAYMENT_PAY,paymentInfo.getOrderId());

        }
    }
}
